<?php

$ref_no = $_POST['ref_no'];
$table_no = $_POST['table_no'];
$mode = $_POST['mode'];

switch ($mode) {
	case 0: delete_reservation($ref_no,$table_no) ;break;
	case 1: confirm_reservation($ref_no,$table_no) ;break;
}

function confirm_reservation($ref_no,$table_no) {
	require_once("../../inc/connect.php");

	$result1 = mysql_query("UPDATE reservation SET status = '1' WHERE no_ref = '$ref_no'");
	$result2 = mysql_query("UPDATE table$table_no SET status = '1' WHERE no_ref = '$ref_no'");

	if($result1 && $result2) {
		$response = "window.location.reload();";
	}else {
		mysql_query("UPDATE reservation SET status = '0' WHERE no_ref = '$ref_no'");
		mysql_query("UPDATE table$table_no SET status = '0' WHERE no_ref = '$ref_no'");

		$response = "alert('ไม่สามารถยืนยันการจองของโต๊ะหมายเลข ' + $table_no + ' รหัสอ้างอิง ' + $ref_no + ' ได้');";
		$response .= "window.location.reload();";
	}

	mysql_close($con);

	header("content-type:text/javascript; charset=utf-8");
	echo $response;
}

function delete_reservation($ref_no,$table_no) {
	require_once("../../inc/connect.php");

	$result1 = mysql_query("DELETE FROM reservation WHERE no_ref = '$ref_no'");
	$result2 = mysql_query("DELETE FROM table$table_no WHERE no_ref = '$ref_no'");

	if($result1 && $result2) {
		$response = "window.location.reload();";
	}else {

		$response = "alert('ไม่สามารถการจองของโต๊ะหมายเลข ' + $table_no + ' รหัสอ้างอิง' + $ref_no + ' ได้');";
		$response .= "window.location.reload();";
	}

	mysql_close($con);

	header("content-type:text/javascript; charset=utf-8");
	echo $response;
}

?>
